package com.snok.emenu;

import java.util.Vector;

import android.opengl.GLES20;

public class DishGrid {
	final String vShaderStatement = 
					"attribute vec4 vPosition;" +
					"attribute vec2 a_TexCoordinate;"+
					"varying vec2 v_TexCoordinate;"+
					"void main() {" +
					"  gl_Position = vPosition;" +
					"  v_TexCoordinate = a_TexCoordinate;"+
					"}";

	final String fShaderStatement = 
					"precision mediump float;" +
					"uniform vec4 vColor;" +
					"void main() {" +
					"  gl_FragColor = vColor;" +
					"}";
	
	final String fTexShaderStatement = 
					"precision mediump float;"+
					"uniform sampler2D u_Texture;"+
					"varying vec2 v_TexCoordinate;"+
					"void main() {" +
					"   gl_FragColor = texture2D(u_Texture, v_TexCoordinate);"+
					"}";
	
	
	
	public int   			glProgram;
	public int				mRowCnt = 1;
	public int				mColCnt = 1;
	
	public static Vector<DishViewPos> sDishViewPosVec;
	public static int				  sViewWidth  = 800;
	public static int				  sViewHeight = 480;

	
	
	
	class DishCtrl{
		ImgRectangle 	mImageRect;
		//TextView		mText;
		//TextView		mPrice;
		//Button		mBtnOrder;
		int				posx;
		int				posy;
		int				sizex;
		int				sizey;
	}
	
	public static class DishViewPos{
		float 			x;
		float 			y;
		float 			fsizex;
		float 			fsizey;
		int				posx;
		int				posy;
		int				isizex;
		int				isizey;
		int				rowcnt;
		int				colcnt;
	}
	
	DishGrid()
	{
		
	}

	public void Init(int width,int height,int rowcnt,int colcnt)
	{
		int 	vshader;
		int 	fshader;
		int[] 	compile_link_status = new int[3];
		int		program; 
		
		sViewWidth = width;
		sViewHeight = height;
		mRowCnt = rowcnt;
		mColCnt = colcnt;
		
		GLES20.glViewport(0, 0, width, height);
		vshader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
		GLES20.glShaderSource(vshader, vShaderStatement);
		GLES20.glCompileShader(vshader);
		
		GLES20.glGetShaderiv(vshader,GLES20.GL_COMPILE_STATUS,compile_link_status,0);
		
		if(0==compile_link_status[0])
		{
			//String infolog = GLES20.glGetShaderInfoLog(vshader);
			GLES20.glDeleteShader(vshader);
			return;
		}
		
		fshader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
		GLES20.glShaderSource(fshader, fTexShaderStatement);
		GLES20.glCompileShader(fshader);
		
		GLES20.glGetShaderiv(fshader,GLES20.GL_COMPILE_STATUS,compile_link_status,1);
		
		if(0==compile_link_status[1])
		{
			//String infolog = GLES20.glGetShaderInfoLog(fshader);
			GLES20.glDeleteShader(vshader);
			GLES20.glDeleteShader(fshader);
			return;
		}
		
		program = GLES20.glCreateProgram();
		
		if(0!=program)
		{
			GLES20.glAttachShader(program, vshader);
	        //checkGlError("glAttachShader");
			GLES20.glAttachShader(program, fshader);
	        //checkGlError("glAttachShader");
			GLES20.glLinkProgram(program);
	        
	        GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS,compile_link_status,2);
	        
	        if(0==compile_link_status[2])
	        {
	        	GLES20.glDeleteProgram(program);
	        	program=0;
	        	return;
	        }
		}
		
		glProgram = program;
		//mImageRect = new ImgRectangle(SnokEmenuConfig.getSDPath()+"/snokemenu/tex.jpg");
	}
	
	public void DrawGridFrame()
	{
		
		GLES20.glClearColor(1.0f,1.0f,1.0f,1.0f);
    	GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    	//mImageRect.draw(glProgram);
	}
	
	public static DishViewPos GetViewPos(int x, int y, int sizex,int sizey,int rowcnt,int colcnt){
		DishViewPos vp = null;
		int i;
		
		for(i = 0 ; i < sDishViewPosVec.size() ; i++){
			DishViewPos vptmp = sDishViewPosVec.elementAt(i);
			if(x == vptmp.x && y == vptmp.y && sizex == vptmp.isizex && sizey == vptmp.isizey &&
					rowcnt == vptmp.rowcnt && colcnt == vptmp.colcnt){
				vp = vptmp;
				break;
			}
		}
		if(null == vp){
			vp = CalcViewPos(x,y,sizex,sizey,rowcnt,colcnt);
			sDishViewPosVec.add(vp);
		}
		return vp;
	}
	
	protected static DishViewPos CalcViewPos(int x, int y, int sizex,int sizey,int rowcnt,int colcnt){
		DishViewPos vp = new DishViewPos();
		vp.x = 0;
		return vp;
	}
}
